using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Acme.BookStore.StockManagement.Dtos.ChaKanGuanLian;
using Acme.BookStore.StockManagement.Services;
using AutoMapper;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Acme.BookStore.Repositories;

namespace Acme.BookStore.StockManagement
{
    /// <summary>
    /// 查看关联应用服务实现类
    /// </summary>
    public class ChaKanGuanLianAppService : ApplicationService, IChaKanGuanLianAppService
    {
        private readonly IRepository<ChaKanGuanLian, int> _chaKanGuanLianRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="chaKanGuanLianRepository">查看关联仓储</param>
        /// <param name="mapper">对象映射</param>
        public ChaKanGuanLianAppService(
            IRepository<ChaKanGuanLian, int> chaKanGuanLianRepository,
            IMapper mapper)
        {
            _chaKanGuanLianRepository = chaKanGuanLianRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取查看关联分页列表
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns>分页结果</returns>
        public async Task<PagedResultDto<ChaKanGuanLianDto>> GetListAsync(ChaKanGuanLianGetListInputDto input)
        {
            // 构建查询条件
            var query = _chaKanGuanLianRepository.GetQueryable();

            // 添加查询条件
            if (!string.IsNullOrEmpty(input.GoodsCodeOrName))
            {
                query = query.Where(x => x.GoodsCode.Contains(input.GoodsCodeOrName) || 
                                         x.GoodsName.Contains(input.GoodsCodeOrName));
            }

            if (!string.IsNullOrEmpty(input.GoodsType))
            {
                query = query.Where(x => x.GoodsType == input.GoodsType);
            }

            if (!string.IsNullOrEmpty(input.Unit))
            {
                query = query.Where(x => x.Unit == input.Unit);
            }

            if (!string.IsNullOrEmpty(input.BatchNumber))
            {
                query = query.Where(x => x.BatchNumber.Contains(input.BatchNumber));
            }

            if (!string.IsNullOrEmpty(input.WarehouseName))
            {
                query = query.Where(x => x.WarehouseName == input.WarehouseName);
            }

            if (input.ProductionDate.HasValue)
            {
                query = query.Where(x => x.ProductionDate.Date == input.ProductionDate.Value.Date);
            }

            if (input.ExpiryDate.HasValue)
            {
                query = query.Where(x => x.ExpiryDate.Date == input.ExpiryDate.Value.Date);
            }

            // 获取总数
            var totalCount = await AsyncExecuter.CountAsync(query);

            // 排序 - 修改为使用Id而非CreationTime
            if (!string.IsNullOrEmpty(input.Sorting))
            {
                query = query.OrderBy(x => x.Id);
            }
            else
            {
                query = query.OrderByDescending(x => x.Id);
            }

            // 分页
            query = query.Skip(input.SkipCount).Take(input.MaxResultCount);

            // 获取数据
            var items = await AsyncExecuter.ToListAsync(query);

            // 转换为DTO
            var dtos = _mapper.Map<List<ChaKanGuanLian>, List<ChaKanGuanLianDto>>(items);

            // 返回分页结果
            return new PagedResultDto<ChaKanGuanLianDto>(totalCount, dtos);
        }

        /// <summary>
        /// 获取查看关联详情
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns>查看关联</returns>
        public async Task<ChaKanGuanLianDto> GetAsync(int id)
        {
            var entity = await _chaKanGuanLianRepository.GetAsync(id);
            return _mapper.Map<ChaKanGuanLian, ChaKanGuanLianDto>(entity);
        }

        /// <summary>
        /// 创建查看关联
        /// </summary>
        /// <param name="input">查看关联</param>
        /// <returns>创建后的查看关联</returns>
        public async Task<ChaKanGuanLianDto> CreateAsync(ChaKanGuanLianDto input)
        {
            var entity = _mapper.Map<ChaKanGuanLianDto, ChaKanGuanLian>(input);
            entity = await _chaKanGuanLianRepository.InsertAsync(entity);
            return _mapper.Map<ChaKanGuanLian, ChaKanGuanLianDto>(entity);
        }

        /// <summary>
        /// 更新查看关联
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="input">查看关联</param>
        /// <returns>更新后的查看关联</returns>
        public async Task<ChaKanGuanLianDto> UpdateAsync(int id, ChaKanGuanLianDto input)
        {
            var entity = await _chaKanGuanLianRepository.GetAsync(id);
            _mapper.Map(input, entity);
            entity = await _chaKanGuanLianRepository.UpdateAsync(entity);
            return _mapper.Map<ChaKanGuanLian, ChaKanGuanLianDto>(entity);
        }

        /// <summary>
        /// 删除查看关联
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _chaKanGuanLianRepository.DeleteAsync(id);
        }
    }
} 